package com.ktgj.service.service.impl;

import com.ktgj.common.core.constant.FileConstants;
import com.ktgj.common.core.text.UUID;
import com.ktgj.common.core.utils.DateUtils;
import com.ktgj.service.domain.Contract;
import com.ktgj.service.domain.ContractAttachment;
import com.ktgj.service.domain.ContractFile;
import com.ktgj.service.mapper.ContractAttachmentMapper;
import com.ktgj.service.mapper.ContractMapper;
import com.ktgj.service.service.IContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 合同			
Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-09-03
 */
@Transactional
@Service
public class ContractServiceImpl implements IContractService 
{
    @Autowired
    private ContractMapper contractMapper;

    //合同附件
    @Autowired
    private ContractAttachmentMapper contractAttachmentMapper;

    /**
     * 查询合同			

     * 
     * @param contractId 合同			
ID
     * @return 合同			

     */
    @Override
    public Contract selectContractById(Long contractId)
    {
        return contractMapper.selectContractById(contractId);
    }

    /**
     * 查询合同			
列表
     * 
     * @param contract 合同			

     * @return 合同			

     */
    @Override
    public List<Contract> selectContractList(Contract contract)
    {
        return contractMapper.selectContractList(contract);
    }

    /**
     * 新增合同			

     * 
     * @param contract 合同

     * @return 结果
     */
    @Override
    public int insertContract(Contract contract)
    {
        contract.setCreateTime(DateUtils.getNowDate());
        int result = contractMapper.insertContract(contract);
        int contractId = 0;
        if(result > 0){
            //返回新增合同后的主键值
             contractId = contract.getContractId().intValue();
        }

        return contractId;
    }

    /**
     * 修改合同
     * @param contract 合同
     * @return 结果
     */
    @Override
    public int updateContract(Contract contract)
    {
        contract.setUpdateTime(DateUtils.getNowDate());
        return contractMapper.updateContract(contract);
    }



    /**
     * 批量删除合同			

     * 
     * @param contractIds 需要删除的合同			
ID
     * @return 结果
     */
    @Override
    public int deleteContractByIds(Long[] contractIds)
    {
        return contractMapper.deleteContractByIds(contractIds);
    }

    /**
     * 删除合同			
信息
     * 
     * @param contractId 合同			
ID
     * @return 结果
     */
    @Override
    public int deleteContractById(Long contractId)
    {
        return contractMapper.deleteContractById(contractId);
    }

    /**
     * 根据服务来源、服务项目，联合查询 合作商(供应商/代理商)、合同数据
     * @param parinerName 服务来源
     * @param invoiceProject 服务项目
     * @return
     */
    @Override
    public List<Contract> selectContractAndParinerList(String parinerName, String invoiceProject) {
        List<Contract> contracts = contractMapper.selectContractAndParinerList(parinerName, invoiceProject);
        return contracts;

    }

    @Value("${file.save-path}")
    private String baseFilePath;

    /**
     * 新增附件
     * @param file
     * @param request
     * @return
     */
    @Override
    public ContractFile importFileContract(MultipartFile file, HttpServletRequest request,
                                           Long contractId,String status) {
        ContractFile contractFile = new ContractFile();
        if (file.isEmpty()) {
            contractFile.setLegal(false);
            return contractFile;
        }
        SimpleDateFormat simpleDateFormat  = new SimpleDateFormat("yyyy-MM-dd/");
        String format = simpleDateFormat.format(new Date());
        File file1 = new File(baseFilePath + format);
        if (!file1.isDirectory()) {
            file1.mkdirs();
        }
        String oldName = file.getOriginalFilename();
        String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."), oldName.length());
        try {
            File newFile = new File(file1.getAbsolutePath() + File.separator + newName);
            file.transferTo(newFile);
            String filePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/picbook/" + FileConstants.UPLOAD_PATH_PREFIX + format + newName;
            int result=0;

            //根据合同id查询合同附件是否存在
            ContractAttachment attachment = contractAttachmentMapper.selectContractAttachmentByContractId(contractId);

            ContractAttachment contractAttachment = new ContractAttachment();
            contractAttachment.setStatus(status);
            contractAttachment.setAttachPath(filePath);
            contractAttachment.setAttachName(newName);
            contractAttachment.setOpemployeeId(1L);
            //存在则是修改操作，不存在则是新增
            if(attachment!=null){
                //存在
                contractAttachment.setUpdateTime(DateUtils.getNowDate());
                contractAttachment.setAttachmentId(attachment.getAttachmentId());
                contractAttachment.setContractId(contractId);

                result = contractAttachmentMapper.updateContractAttachment(contractAttachment);
            }else{
                //不存在
                contractAttachment.setCreateTime(DateUtils.getNowDate());
                contractAttachment.setUpdateTime(DateUtils.getNowDate());
                contractAttachment.setContractId(contractId);

                result = contractAttachmentMapper.insertContractAttachment(contractAttachment);
            }

            if(result > 0){
                contractFile.setLegal(true);
                contractFile.setIsPath(filePath);
            }
            return contractFile;
        } catch (Exception e) {
            e.printStackTrace();
        }
        contractFile.setLegal(false);
        return contractFile;
    }


}
